﻿using System;

namespace Framework
{
    public class ObjHandle<T> : IEquatable<ObjHandle<T>> where T : class
    {
        public T _handleObj;
        public ObjHandle(T obj)
        {
            this._handleObj = obj;
        }

        public bool IsValid
        {
            get
            {
                return _handleObj != null;
            }
            
        }

        public void Release()
        {
            this._handleObj = null;
        }

        public bool Equals(ObjHandle<T> other)
        {
            return (this == other);
        }

        public override bool Equals(object obj)
        {
            return obj != null && GetType() == obj.GetType() && this == (ObjHandle<T>)obj;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public T handle
        {
            get
            {
                return this._handleObj;
            }
        }

        public static bool operator ==(ObjHandle<T> lhs, ObjHandle<T> rhs)
        {
            return lhs._handleObj == rhs._handleObj;
        }

        public static bool operator !=(ObjHandle<T> lhs, ObjHandle<T> rhs)
        {
            return lhs._handleObj != rhs._handleObj;
        }

        public static ObjHandle<T> NULL = new ObjHandle<T>(null);
    }
}

